package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.common.utils.ConvertLatlng;
import com.mingtu.common.utils.GridSpacingItemDecoration;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.bean.AllSignInRecordBean;

import java.util.ArrayList;
import java.util.List;

public class MySignInRecordAdapter extends BaseQuickAdapter<AllSignInRecordBean.PageBean.ListBean, BaseViewHolder> {

    private Context context;

    public MySignInRecordAdapter(Context context) {
        super(R.layout.item_my_sign_in_record);
        this.context = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, AllSignInRecordBean.PageBean.ListBean listBean) {
        String clockTime = listBean.getClockTime();
        String lnglat = listBean.getLnglat();
        boolean isUpload = listBean.isUpload();
        List<AllSignInRecordBean.PageBean.ListBean.TaskClockAttachEntityListBean> entityList = listBean.getTaskClockAttachEntityList();


        LinearLayout layout = helper.getView(R.id.layout);
        RecyclerView recycler = helper.getView(R.id.recycler);
        ImageView defaultImg = helper.getView(R.id.default_img);
        TextView tvStatus = helper.getView(R.id.tv_status);
        if (isUpload) {
            tvStatus.setVisibility(View.GONE);
            tvStatus.setText("已上传");
            tvStatus.setSelected(true);
        } else {
            tvStatus.setVisibility(View.VISIBLE);
            tvStatus.setText("待上传");
            tvStatus.setSelected(false);
        }


        GridLayoutManager layoutManager = new GridLayoutManager(context, 3);//第二个参数为网格的列数
        recycler.setLayoutManager(layoutManager);
        if (recycler.getItemDecorationCount() > 0) { //解决RecycleView嵌套后重复调用addItemDecoration方法使间隔增大
            RecyclerView.ItemDecoration itemDecorationAt = recycler.getItemDecorationAt(0);
            if (itemDecorationAt == null) {
               recycler.addItemDecoration(new GridSpacingItemDecoration(2, SizeUtils.dp2px(15), false));
            }
        } else {
            //需要在setLayoutManager()之后调用addItemDecoration()
            recycler.addItemDecoration(new GridSpacingItemDecoration(2, SizeUtils.dp2px(15), false));
        }


        List<AllSignInRecordBean.PageBean.ListBean.TaskClockAttachEntityListBean> entityListBeanList = listBean.getTaskClockAttachEntityList();

        if (entityListBeanList.size() > 0) {
            defaultImg.setVisibility(View.GONE);
            recycler.setVisibility(View.VISIBLE);
            final ArrayList<LocalMedia> selectPictureList = new ArrayList<>();
            for (int i = 0; i < entityListBeanList.size(); i++) {
                String url = entityListBeanList.get(i).getUrl();
                LocalMedia localMedia = null;
                if(PictureMimeType.isHasHttp(url)){
                     localMedia = LocalMedia.generateHttpAsLocalMedia(url);
                }else{
                     localMedia = LocalMedia.generateLocalMedia(context, url);
                }
                selectPictureList.add(localMedia);
            }
            SignInPictureVideoAdapter signInPictureVideoAdapter = new SignInPictureVideoAdapter(context, entityListBeanList);
            recycler.setAdapter(signInPictureVideoAdapter);
            //此逻辑是为了解决点击冲突
            recycler.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return layout.onTouchEvent(event);
                }
            });
            signInPictureVideoAdapter.setOnItemClickListener(new SignInPictureVideoAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    BaseApplication.initPreviewModel().startActivityPreview(position, false, selectPictureList);
                }
            });
        } else {
            defaultImg.setVisibility(View.VISIBLE);
            recycler.setVisibility(View.GONE);
            setParams(defaultImg);
        }

        if (!StringUtils.isEmpty(clockTime)) {
            helper.setText(R.id.tv_time, "签到时间：" + clockTime);
        }

        if (!StringUtils.isEmpty(lnglat)) {
            if (lnglat.indexOf(",") != -1) {
                String[] array = lnglat.split(",");

                double lng = Double.parseDouble(array[0]);
                double lat = Double.parseDouble(array[1]);
                String myLng = ConvertLatlng.getInstance().convertToSexagesimal(lng);
                String myLat = ConvertLatlng.getInstance().convertToSexagesimal(lat);
                helper.setText(R.id.tv_location, "经纬度：" + myLng + "," + myLat);
            }
        }


    }

    private void setParams(ImageView defaultImg) {
        ViewGroup.LayoutParams lp = defaultImg.getLayoutParams();
        int screenWidth = ScreenUtils.getScreenWidth();
        int mWidth = (screenWidth - SizeUtils.dp2px(20) * 2 - SizeUtils.dp2px(15) * 4) / 3;
        lp.width = mWidth;
        lp.height = mWidth;
        defaultImg.setLayoutParams(lp);
    }

}
